package com.aurora.service.impl;

import com.alibaba.fastjson.JSON;
import com.aurora.model.dto.EmailDTO;
import com.aurora.model.dto.FriendLinkAdminDTO;
import com.aurora.model.dto.FriendLinkDTO;
import com.aurora.entity.FriendLink;
import com.aurora.mapper.FriendLinkMapper;
import com.aurora.model.vo.FriendLinkReviewVO;
import com.aurora.service.FriendLinkService;
import com.aurora.util.BeanCopyUtil;
import com.aurora.util.EmailUtil;
import com.aurora.util.PageUtil;
import com.aurora.model.vo.ConditionVO;
import com.aurora.model.vo.FriendLinkVO;
import com.aurora.model.dto.PageResultDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.aurora.util.CommonUtil.checkEmail;
@Service
public class FriendLinkServiceImpl extends ServiceImpl<FriendLinkMapper, FriendLink> implements FriendLinkService {

    @Autowired
    private FriendLinkMapper friendLinkMapper;
    @Value("${friendLink.url}")
    private String url;
    @Value("${friendLink.email}")
    private String email;

    @Autowired
    private EmailUtil emailUtil;

    private static final Logger logger = LoggerFactory.getLogger(FriendLinkServiceImpl.class);

    @Override
    public List<FriendLinkDTO> listFriendLinks() {
        LambdaQueryWrapper<FriendLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FriendLink::getIsReview, 1);
        List<FriendLink> friendLinks = friendLinkMapper.selectList(queryWrapper);
        return BeanCopyUtil.copyList(friendLinks, FriendLinkDTO.class);
    }

    @Override
    public PageResultDTO<FriendLinkAdminDTO> listFriendLinksAdmin(ConditionVO conditionVO) {
        Page<FriendLink> page = new Page<>(PageUtil.getCurrent(), PageUtil.getSize());
        Page<FriendLink> friendLinkPage = friendLinkMapper.selectPage(page, new LambdaQueryWrapper<FriendLink>()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), FriendLink::getLinkName, conditionVO.getKeywords()));
        List<FriendLinkAdminDTO> friendLinkBackDTOs = BeanCopyUtil.copyList(friendLinkPage.getRecords(), FriendLinkAdminDTO.class);
        return new PageResultDTO<>(friendLinkBackDTOs, (int) friendLinkPage.getTotal());
    }

    @Override
    public void saveOrUpdateFriendLink(FriendLinkVO friendLinkVO) {
        FriendLink friendLink = BeanCopyUtil.copyObject(friendLinkVO, FriendLink.class);
        this.saveOrUpdate(friendLink);
    }


    @Override
    public void applyLink(FriendLinkVO friendLinkVO) {
        FriendLink friendLink = BeanCopyUtil.copyObject(friendLinkVO, FriendLink.class);
        String domain = extractDomain(friendLink.getLinkAddress());
        if (domain != null) {
            // 使用 friendLinkMapper 查询是否存在包含该域名的记录
            LambdaQueryWrapper<FriendLink> queryWrapper = new LambdaQueryWrapper<FriendLink>()
                    .like(FriendLink::getLinkAddress, domain);
            boolean exists = friendLinkMapper.selectCount(queryWrapper) > 0;
            // 根据域名查询数据库中是否存在该记录SELECT COUNT(*)FROM friend_link WHERE link_address LIKE '%domain%';
            if (exists) {
                // 处理域名已存在的逻辑
                logger.info("友链地址 {} 已存在", domain);
            } else {
                // 处理域名不存在的逻辑
                friendLink.setIsReview(0); // 设置审核状态为未审核
                this.save(friendLink); // 使用 this.save 保存新的友链记录
                logger.info("友链地址 {} 不存在，已保存", domain);
                sendReviewEmail(friendLink); // 发送审核邮件
            }
        } else {
            // 处理无法提取域名的逻辑
            logger.error("无法提取友链地址的域名: {}", friendLink.getLinkAddress());
        }
        // todo 其他逻辑...

    }
    private String extractDomain(String url) {
        try {
            URI uri = new URI(url);
            String domain = uri.getHost();
            if (domain != null) {
                // 去除 www 前缀
                return domain.startsWith("www.") ? domain.substring(4) : domain;
            }
        } catch (URISyntaxException e) {
            logger.error("无效的URL: {}", url, e);
        }
        return null;
    }
    public void sendReviewEmail(FriendLink friendLink) {
        // 生成审核链接
        String reviewUrl = url +"/api/review/" + friendLink.getId();
        String approveUrl = reviewUrl+ "?action=approve";
        String rejectUrl = reviewUrl+ "?action=reject";
        // 构建邮件内容的变量
        Map<String, Object> map = new HashMap<>();
        map.put("linkName", friendLink.getLinkName());
        map.put("linkAvatar", friendLink.getLinkAvatar());
        map.put("linkAddress", friendLink.getLinkAddress());
        map.put("linkIntro", friendLink.getLinkIntro());
        map.put("approveUrl", approveUrl);
        map.put("rejectUrl", rejectUrl);
        // 构建邮件对象
        EmailDTO emailDTO = EmailDTO.builder()
                .email(email)
                .subject("友链审核提醒")
                .commentMap(map)
                .template("friend_link_review.html")
                .build();
        logger.info("友链审核邮件内容：{}", JSON.toJSONString(emailDTO));
        if (!checkEmail(email)) {
            logger.error("邮箱格式错误：{}", email);
            return;
        }
        emailUtil.sendHtmlMail(emailDTO);
        // 发送邮件
        logger.info("发送邮件至：{}，邮件主题：{}", emailDTO.getEmail(), emailDTO.getSubject());
    }

    @Override
    public void reviewFriendLink(FriendLinkReviewVO friendLinkReviewVO) {
        Integer linkId = friendLinkReviewVO.getLinkId();
        Integer isReview = friendLinkReviewVO.getIsReview();
        FriendLink friendLink = new FriendLink();
        friendLink.setId(linkId);
        friendLink.setIsReview(isReview);
        this.updateById(friendLink);
        if (isReview == 1) {
            logger.info("友链审核通过：ID={}", linkId);
        } else {
            logger.info("友链审核不通过：ID={}", linkId);
        }
        logger.info("友链审核结果：ID={}，审核状态={}", linkId, isReview);
    }

}
